package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.net.SuccessConsumer;

import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.bean.BaizhubaoListBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.BaizhubiaoListContract;
import top.hidoctor.auscultation.net.req.BaizhubaoReq;

public class BaizhubaoPresenter extends PresenterWrapper<BaizhubiaoListContract.View> implements BaizhubiaoListContract.Presenter {
    public BaizhubaoPresenter(Context context, BaizhubiaoListContract.View view) {
        super(context, view);
    }

    @Override
    public void getList(BaizhubaoReq req, SwipeRefreshLayout refreshLayout) {
        if (refreshLayout != null && !refreshLayout.isRefreshing()) {
            mView.showLoading();
        }
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        add(mService.label(map)
                .compose(this.<BaizhubaoListBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<BaizhubaoListBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<BaizhubaoListBean> response) {
                        mView.hideLoading();
                        mView.getListSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void deleteLabel(String id) {
        mView.showLoading();
        add(mService.deleteLabel(id, new NetMapEncryptUtils("").getMaps())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.deleteLabelSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }
}
